<template>
  <div class="history-container">
    <div class="history-header">
      <h2>观看历史</h2>
      <el-button type="text" @click="clearHistory">清空历史</el-button>
    </div>

    <div class="history-list">
      <div
        v-for="item in historyList"
        :key="item.id"
        class="history-item"
        @click="playVideo(item)"
      >
        <img :src="item.cover" :alt="item.title" class="history-cover" />
        <div class="history-info">
          <h3>{{ item.title }}</h3>
          <p class="history-progress">观看至 {{ item.progress }}</p>
          <p class="history-time">{{ item.watchTime }}</p>
        </div>
        <el-button text @click.stop="removeFromHistory(item.id)">
          <el-icon><Delete /></el-icon>
        </el-button>
      </div>
    </div>

    <div v-if="historyList.length === 0" class="empty-state">
      <el-icon size="64"><Clock /></el-icon>
      <p>暂无观看历史</p>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { Clock, Delete } from '@element-plus/icons-vue'

const router = useRouter()

const historyList = ref([
  {
    id: 1,
    title: '庆余年 第二季 第1集',
    cover: '/covers/history1.jpg',
    progress: '25:30',
    watchTime: '今天 14:30'
  },
  {
    id: 2,
    title: '繁花 第5集',
    cover: '/covers/history2.jpg',
    progress: '42:15',
    watchTime: '昨天 20:45'
  },
  {
    id: 3,
    title: '三体 第12集',
    cover: '/covers/history3.jpg',
    progress: '18:20',
    watchTime: '2天前 19:15'
  }
])

const playVideo = (item: any) => {
  router.push({
    path: '/player',
    query: { url: item.id }
  })
}

const removeFromHistory = (id: number) => {
  const index = historyList.value.findIndex(item => item.id === id)
  if (index > -1) {
    historyList.value.splice(index, 1)
  }
}

const clearHistory = () => {
  historyList.value = []
}
</script>

<style lang="scss" scoped>
.history-container {
  padding: 20px;
  height: 100%;
  overflow-y: auto;
}

.history-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;

  h2 {
    font-size: 24px;
  }
}

.history-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.history-item {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 12px;
  background: var(--tx-bg-secondary);
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;

  &:hover {
    background: var(--tx-bg-tertiary);
  }

  .history-cover {
    width: 120px;
    height: 68px;
    border-radius: 4px;
    object-fit: cover;
  }

  .history-info {
    flex: 1;

    h3 {
      font-size: 16px;
      margin-bottom: 4px;
    }

    .history-progress {
      font-size: 14px;
      color: #ff6022;
      margin-bottom: 2px;
    }

    .history-time {
      font-size: 12px;
      color: #b3b3b3;
    }
  }
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 400px;
  color: #b3b3b3;

  p {
    margin-top: 16px;
    font-size: 16px;
  }
}
</style>
